package com.webchat.chat.web;
import com.webchat.chat.core.baseEntity.Result;
import com.webchat.chat.core.baseEntity.ResultGenerator;
import com.webchat.chat.core.tools.ChatConstant;
import com.webchat.chat.core.tools.ShiroUtil;
import com.webchat.chat.model.Tenant;
import com.webchat.chat.service.TenantService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by admin on 2018-06-29.
*/
@RestController
@RequestMapping(ChatConstant.CONTROLLER_ROOT_PATH + "/tenant")
public class TenantController extends BaseController {
    @Resource
    private TenantService tenantService;

    @PostMapping("/add")
    public Result add(Tenant tenant) {
        tenantService.save(tenant);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam String id) {
        if (!"All".equals(ShiroUtil.getTenantCode())) {
            return ResultGenerator.genFailResult("没有对应操作权限，有需要请联系管理员！！");
        }
        tenantService.deleteTenantById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(Tenant tenant) {
        if (!"All".equals(ShiroUtil.getTenantCode())) {
            return ResultGenerator.genFailResult("没有对应操作权限，有需要请联系管理员！！");
        }
        tenantService.update(tenant);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam String id) {
        Tenant tenant = tenantService.findById(id);
        return ResultGenerator.genSuccessResult(tenant);
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        if (!"All".equals(ShiroUtil.getTenantCode())) {
            return ResultGenerator.genFailResult("没有对应操作权限，有需要请联系管理员！！");
        }
        PageHelper.startPage(page, size);
        List<Tenant> list = tenantService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @PostMapping("/init")
    public Result init(@RequestParam String tenantCode) {
        Tenant tenant = tenantService.findBy("tenantCode", tenantCode);
        return ResultGenerator.genSuccessResult(tenant);
    }
}
